package br.com.simora.http;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import javax.inject.Inject;
import javax.inject.Named;

import br.com.simora.arquivo.ArquivoDAO;
import br.com.simora.execucao.LogErroCapturaDAO;
import br.com.simora.radio.Radio;

@Named("rtmpHelper")
public class RtmpHelper {
	@Inject private LogErroCapturaDAO logErroCapturaDAO;
	@Inject private ArquivoDAO arquivoDAO;

	public String capturaAudio(Radio radio) {
		Boolean capturouAudio = false;
		File f = arquivoDAO.criaArquivoTimestamp(radio.getNome());
		String caminhoCompletoSemExtensao = f.getAbsolutePath().replace(".mp3", "");
		try {
	        ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-y", "-i",
	        		radio.getUrl(), "-ss", "00:00:00",
	                "-t", "00:00:05", "-c:a", "copy", caminhoCompletoSemExtensao + ".m4a");
	        Process process = processBuilder.start();
	        InputStream stderr = process.getErrorStream();
	        InputStreamReader isr = new InputStreamReader(stderr);
	        BufferedReader br = new BufferedReader(isr);
	        while ((br.readLine()) != null);
	        process.waitFor();
	        
	        capturouAudio = true;
	        try {
	            process.destroy();
	            isr.close();
	            stderr.close();
	
	        } catch (Exception e) {
	        	logErroCapturaDAO.gravaErro(radio, e.getMessage(), new Date());
	            e.printStackTrace();
	        }
		} catch (IOException | InterruptedException e) {
			logErroCapturaDAO.gravaErro(radio, e.getMessage(), new Date());
            e.printStackTrace();
        }
		
		if(capturouAudio){
			if(converteM4aMp3(radio, caminhoCompletoSemExtensao)){
				return f.getAbsolutePath();
			} else {
				return null;
			}
		} else {
			return null;
		}
        
    }
	
	private Boolean converteM4aMp3(Radio radio, String caminhoCompletoSemExtensao){
		Boolean converteuAudio = false;
		try {
		 ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-y", "-i",
				 caminhoCompletoSemExtensao +".m4a", "-acodec", "libmp3lame",
	                "-ab", "128k", caminhoCompletoSemExtensao + ".mp3");
	        Process process = processBuilder.start();
	        InputStream stderr = process.getErrorStream();
	        InputStreamReader isr = new InputStreamReader(stderr);
	        BufferedReader br = new BufferedReader(isr);
	        while ((br.readLine()) != null);
	        process.waitFor();

	        converteuAudio = true;
	        arquivoDAO.deletaArquivo(caminhoCompletoSemExtensao + ".m4a");
	        try {
	            process.destroy();
	            isr.close();
	            stderr.close();

	        } catch (Exception e) {
	        	logErroCapturaDAO.gravaErro(radio, e.getMessage(), new Date());
	            e.printStackTrace();
	        }
		 } catch (IOException | InterruptedException e) {
			 logErroCapturaDAO.gravaErro(radio, e.getMessage(), new Date());
            e.printStackTrace();
        }
		return converteuAudio;
	}
	
}